//
//  ZMLeftTableView.swift
//  LoveBeen
//
//  Created by pg on 2017/8/7.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit

class ZMLeftTableView: UITableView {

    //向外部传递cell点击事件
    var leftTabelViewCellDidSelected:((_ cellIndexPath: IndexPath)->())?
    
    var titleArray:[String]?{
        didSet{
            
            
            self.reloadData()
            //默认选中第0行
            let indexpath = IndexPath(row: 0, section: 0)
            self.selectRow(at: indexpath, animated: false, scrollPosition: UITableViewScrollPosition.top)
        }
    }
    
    
    
    override init(frame: CGRect, style: UITableViewStyle) {
        super.init(frame: frame, style: style)
        
        delegate = self
        dataSource = self
        
        //注册cell
        register(leftTableviewCell.self, forCellReuseIdentifier: "cell")
        
       
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
extension ZMLeftTableView : UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titleArray?.count ?? 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! leftTableviewCell
        
        cell.titleText = titleArray![indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //向外部传递cell点击事件
      
        leftTabelViewCellDidSelected?(indexPath)
    }
    
}

class leftTableviewCell: UITableViewCell {
    
    //cell数据源
    var titleText: String?{
        didSet{
            titleLabel.text = titleText
        }
    }
    
    //懒加载控件
    fileprivate lazy var titleLabel:UILabel = {
       
        let title = UILabel()
        title.font = UIFont.systemFont(ofSize: 14)
        
        
        return title
    }()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //添加label
        self.contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.center.equalTo(contentView)
            
        }
        
        self.selectedBackgroundView = leftTableviewCellSelectedView()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
}


class leftTableviewCellSelectedView: UIView {
    
    override func draw(_ rect: CGRect) {
        
        let rectPath = UIBezierPath(rect: rect)
        UIColor.lightGray.setFill()
        rectPath.fill()
        
        //左侧橙色矩形
        let rectX : CGFloat = 0
        let rectH : CGFloat = rect.size.height * 0.7
        let rectW : CGFloat = 4
        let rectY: CGFloat = (rect.size.height - rectH) * 0.5
        let leftBezierPath = UIBezierPath(rect: CGRect(x: rectX, y: rectY, width: rectW, height: rectH))
        
        UIColor.orange.setFill()
        leftBezierPath.fill()
        
    }
    
}


